package com.jk.smartelderlycare.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.jk.smartelderlycare.dto.SysGoods;
import com.jk.smartelderlycare.dto.SysSupplier;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.service.SysSupplierServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysSupplierController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/12/5 11:57
 */
@Slf4j
@Api(tags = "供应商管理模块")
@RestController
@RequestMapping(value = "supplier")
public class SysSupplierController {

    @Autowired
    public SysSupplierServiceFeign sysSupplierServiceFeign;

    /**
     * @description: 查询所有商品
     * @param: [sysGoods, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysGoods>>
     * @author hhk
     * @date: 2024/12/5 11:20
     */
    @ApiOperation(value = "查询所有商品",httpMethod = "POST",notes = "查询所有商品")
    @PostMapping(value = "findSysSupplierPaginationList")
    public ResponseResult<PagedResponse<SysSupplier>> findSysSupplierPaginationList(@RequestBody SysSupplier sysSupplier, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysSupplierServiceFeign.findSysSupplierPaginationList(sysSupplier,pageNum,pageSize));
    }

    /**
     * @description: 信息导出
     * @param: [video, response]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody SysSupplier sysSupplier, HttpServletResponse response){
        List<SysSupplier> SupplieList = sysSupplierServiceFeign.findSysSupplierList();

        //.xlsx格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setContentType("application/octet-stream; charset=UTF-8");


        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("信息表");
            //设置标题
            params.setTitle("信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, SysSupplier.class, SupplieList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * @description: 查询供应商
     * @param: []
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<java.util.List<com.jk.smartelderlycare.dto.SysGoods>>
     * @author hhk
     * @date: 2024/12/5 12:35
     */
    @ApiOperation(value = "查询供应商",httpMethod = "GET",notes = "查询供应商")
    @GetMapping(value = "findSysSupplierList")
    public ResponseResult<List<SysSupplier>> findSysSupplierList(){
        return new ResponseResult<>(200,"查询成功",sysSupplierServiceFeign.findSysSupplierList());
    }


}
